/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.core.model;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.anderground.core.util.Coord;

/**
 * Contains info about a city, such as name, location, aliases and country.
 * 
 * @see #getName()
 * @see #getLocation()
 * @see #getCountry()
 * 
 * @author Eldar Abusalimov
 */
public class City implements Serializable {

	private static final long serialVersionUID = -58122374802013567L;

	/**
	 * @author Eldar Abusalimov
	 */
	public final class Editor {

		/**
		 * private constructor
		 */
		private Editor() {
		}

		/**
		 * @param name
		 *            the name to set
		 */
		public void setName(String name) {
			City.this.name = name == null ? DEFAULT_NAME : name;
		}

		/**
		 * @param country
		 *            the country to set
		 */
		public void setCountry(Country country) {
			City.this.country = country == null ? DEFAULT_COUNTRY : country;
		}

		/**
		 * @param aliases
		 *            the aliases to set
		 */
		public void setAliases(List<String> aliases) {
			City.this.aliases = aliases == null ? new HashSet<String>()
					: new HashSet<String>(aliases);
			unmodifiableAliasesSet = Collections
					.unmodifiableSet(City.this.aliases);
		}

		/**
		 * @param location
		 *            the location to set
		 */
		public void setLocation(Coord location) {
			City.this.location = location == null ? new Coord() : location;
		}

	}

	public static final String DEFAULT_NAME = "Untitled City";
	public static final Country DEFAULT_COUNTRY = new Country();

	/** the name of the city represented in this Model */
	private String name;

	/** city name aliases */
	private Set<String> aliases;

	/** unmodifiable set of the aliases backed by the {@link #aliases} set */
	private Set<String> unmodifiableAliasesSet;

	/** country information */
	private Country country;

	/** geographical coordinates */
	private Coord location;

	transient private Editor editor;

	/**
	 * default constructor
	 */
	public City() {
		this(null, null, null, null);
	}

	/**
	 * @param name
	 *            the name of the city
	 * @param aliases
	 * @param country
	 * @param location
	 */
	public City(String name, List<String> aliases, Country country,
			Coord location) {
		this.name = name == null ? DEFAULT_NAME : name;
		this.aliases = aliases == null ? new HashSet<String>()
				: new HashSet<String>(aliases);
		this.country = country == null ? DEFAULT_COUNTRY : country;
		this.location = location == null ? new Coord() : location;

		unmodifiableAliasesSet = Collections.unmodifiableSet(this.aliases);
	}

	/**
	 * @return
	 */
	Editor getEditor() {
		if (editor == null) {
			editor = new Editor();
		}

		return editor;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the country
	 */
	public Country getCountry() {
		return country;
	}

	/**
	 * @return the unmodifiable list of the city's aliases
	 */
	public Set<String> getAliases() {
		return unmodifiableAliasesSet;
	}

	/**
	 * @return the location
	 */
	public Coord getLocation() {
		return location;
	}

	@Override
	public String toString() {
		return name;
	}

	@Override
	public int hashCode() {
		return name.hashCode() + aliases.hashCode() + country.hashCode()
				+ location.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		City another;
		if (obj instanceof City) {
			another = (City) obj;
		} else {
			return false;
		}

		if (!name.equals(another.name)) {
			return false;
		}
		if (!aliases.equals(another.aliases)) {
			return false;
		}
		if (!country.equals(another.country)) {
			return false;
		}
		if (!location.equals(another.location)) {
			return false;
		}

		return true;
	}

}
